package com.ztx.web.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztx.model.ResourceType;
import com.ztx.model.ResourceVO;
import com.ztx.pagemodel.SessionInfo;
import com.ztx.pagemodel.easyui.Json;
import com.ztx.pagemodel.easyui.Tree;
import com.ztx.service.base.IBaseService;
import com.ztx.service.resource.IResourceService;

@Controller
@RequestMapping("/resourceController")
public class ResourceController {

	@Resource
	private IResourceService resourceService;

	@Resource
	private IBaseService baseService;

	public void setResourceService(IResourceService resourceService) {
		this.resourceService = resourceService;
	}

	/**
	 * 查询菜单树数据
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree(HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session
				.getAttribute("sessionInfo");
		return resourceService.tree(sessionInfo);
	}

	/**
	 * 跳转到资源管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String manager() {
		return "admin/resource";
	}

	/**
	 * 
	 * 资源管理界面表格数据
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/treeGrid", method = RequestMethod.POST)
	@ResponseBody
	public List<ResourceVO> treeGrid(HttpSession session) {
		List<ResourceVO> list = null;
		try {
			list = baseService.selectList(ResourceVO.class.getName()
					+ ".getResourceAndType");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 查询资源类型
	 * 
	 * @return
	 */
	@RequestMapping(value = "getResourceType", method = RequestMethod.POST)
	@ResponseBody
	public List<ResourceType> getResourceType() {
		List<ResourceType> list = null;
		try {
			list = baseService.selectList(ResourceVO.class.getName()
					+ ".getResourceType");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 获取父类资源
	 * 
	 * @return
	 */
	@RequestMapping(value = "getParentResource", method = RequestMethod.POST)
	@ResponseBody
	public List<ResourceVO> getParentResource() {
		List<ResourceVO> list = null;
		try {
			list = baseService.selectList(ResourceVO.class.getName()
					+ ".getParentResource");
			ResourceVO r = new ResourceVO();
			r.setId("");
			r.setName("空");
			list.add(0,r);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 增加资源
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addResource", method = RequestMethod.POST)
	@ResponseBody
	public Json addResource(ResourceVO resource) {
		Json json = new Json();
		try {
			baseService.add(ResourceVO.class.getName() + ".addResource",
					resource);
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg("操作失败," + e.getMessage());
		}
		return json;
	}
}
